import pytest
from test_case import TestSgcc
import time
from const import user_data, o_data
from selenium.webdriver.common.by import By as by
import json
import sys
import os


class TestDemo(TestSgcc):

    @staticmethod
    def init():
        print('开始初始化')
        if not os.path.exists(o_data['screen_path']):
            print('创建图片缓存文件夹')
            os.makedirs(o_data['screen_path'])

    # 登录用例
    @pytest.mark.skip(reason='跳过 该用例')
    def test_01(self):
        # 获取username输入框
        user_edit = self.web.find_element_by_xpath('//*[@id="username"]')
        pass_edit = self.web.find_element_by_xpath('//*[@id="j_password"]')

        # print(user_edit)
        # print(pass_edit)

        user_edit.send_keys(user_data['user'])
        pass_edit.send_keys(user_data['pass'])

        # ocr识别验证码
        x_code_element = self.web.find_element_by_xpath('//*[@id="captchaimg"]')
        x_code_img = x_code_element.get_attribute('src')
        print(f'x_code_img -->>  {x_code_img}')
        # 获取坐标与尺寸
        location = x_code_element.location
        size = x_code_element.size
        print(f'location -->> {location}')
        print(f'size -->>  {size}')
        # 开始截图
        # print('开始截图')
        print(self.screenshot())
        # 开始裁剪
        self.img_crop(os.path.join(o_data['screen_path'], 'temp.png'),
                      os.path.join(o_data['screen_path'], 'x_code.png'),
                      location['x'],
                      location['y'],
                      size['width'],
                      size['height']
                      )
        # 开始识别
        # result = self.img_to_str(os.path.join(o_data['screen_path'], 'x_code.png'))
        # print(f'识别结果为-->>  {result}')
        # 等待手动输入验证码
        self.web.find_element_by_xpath('//*[@id="captcha"]').click()
        while 1 == 1:
            xcode_edit = self.web.find_element_by_xpath('//*[@id="captcha"]')
            # print(xcode_edit)
            xcode_content = xcode_edit.get_attribute('value')
            print(f'xcode_content -->> {xcode_content}')
            # print(len(xcode_content))
            if len(xcode_content) == 4:
                break
            time.sleep(1)
        # 获取登录按钮
        login_button = self.web.find_element_by_xpath('//*[@id="J_login"]')
        login_button.click()

        # 判断是否登录成功
        self.web.find_element_by_xpath(f'//span[@title="{user_data["name"]}"]')

        print('登录成功')
        # 写入cookie
        time.sleep(5)
        cookie_str = self.web.get_cookies()
        cookie_json = json.dumps(cookie_str)
        # print(jsonCookies)
        with open(file='cookie.txt', mode='w') as f:
            f.write(cookie_json)
            print('写入cookie成功')

    # 增
    @pytest.mark.skip(reason='跳过 该用例')
    def test_02(self):
        print('02')

        self.test_01()
        self.web.get('https://admin.rmt.sc.sgcc.com.cn/a#!/a/wechatv2/news/tree?label=zhandian')
        self.web.refresh()

        print('开始切换')
        self.web.switch_to.frame(self.web.find_element_by_xpath('//*[@id="admui-pageContent"]/iframe[2]'))
        print('切换完成')
        # 测试栏目
        self.web.find_element_by_xpath('//*[@id="JS_slidemenu_ztree_3_a"]').click()
        # 测试投屏
        self.web.find_element_by_xpath('//*[@id="JS_slidemenu_ztree_5_a"]').click()
        # self.web.switch_to.default_content()
        # 进入ifame
        self.web.switch_to.frame('JS_page_inner_iframe')
        # 新增
        self.web.find_element_by_xpath('/html/body/div[1]/div/div/div/div[1]/div/div[1]/a').click()
        # 引用标题
        self.web.find_element_by_xpath('//*[@id="citeTitle"]').send_keys(o_data['qaout_title'])
        # 主标题
        self.web.find_element_by_xpath('//*[@id="title"]').send_keys(o_data['title'])
        # 副标题
        self.web.find_element_by_xpath('//*[@id="subTitle"]').send_keys(o_data['sub_title'])
        # 列表标题
        self.web.find_element_by_xpath('//*[@id="listTitle"]').send_keys(o_data['list_title'])
        # 摘要
        self.web.find_element_by_xpath('//*[@id="summary"]').send_keys(o_data['abstract'])
        # 来源
        self.web.find_element_by_xpath('//*[@id="copyfrom"]').send_keys(o_data['news_where'])
        # 切换iframe
        self.web.switch_to.frame('ueditor_0')
        # 输入内容
        self.web.find_element_by_xpath('/html/body').send_keys(o_data['content'])
        self.web.switch_to.parent_frame()
        # 保存
        self.web.find_element_by_xpath('//*[@id="JS_edit_save_btn"]').click()
        # content_send[0].click()
        # time.sleep(3)

        # self.web.get('https://admin.rmt.sc.sgcc.com.cn/')
        # if os.path.exists('cookie.txt'):
        #     print('开始读取cookie')
        #     with open(file='cookie.txt', mode='r', encoding='utf-8') as f:
        #         cookies_json = json.loads(f.read())
        #         print(cookies_json)
        #         print('开始遍历cookie')
        #         for c in cookies_json:
        #             print(c)
        #             print('------------')
        #             print(type(c))
        #             self.web.add_cookie(c)
        #         # 刷新
        #         print('刷新')
        #         self.web.refresh()
        #         self.web.get(o_data['news'])

        # time.sleep(10)

    # 改
    def test_03(self):
        self.test_01()
        self.web.get('https://admin.rmt.sc.sgcc.com.cn/a#!/a/wechatv2/news/tree?label=zhandian')
        self.web.refresh()
        print('开始切换')
        self.web.switch_to.frame(self.web.find_element_by_xpath('//*[@id="admui-pageContent"]/iframe[2]'))
        print('切换完成')
        # 测试栏目
        self.web.find_element_by_xpath('//*[@id="JS_slidemenu_ztree_3_a"]').click()
        # 测试投屏
        self.web.find_element_by_xpath('//*[@id="JS_slidemenu_ztree_5_a"]').click()
        # self.web.switch_to.default_content()
        # 进入ifame
        self.web.switch_to.frame('JS_page_inner_iframe')
        # 等待提审
        time.sleep(3)
        self.web.find_element_by_xpath('//a[contains(text(), "提审")]').click()
        time.sleep(3)

        # 勾选第一个
        r = self.web.find_element_by_xpath(f'//span[@data-content="{o_data["title"]}"]/parent::*/parent::*')
        b = r.find_element_by_xpath('//a[text()="修改"]').click()

        time.sleep(3)


if __name__ == '__main__':
    TestDemo.init()
    pytest.main(['-s', 'main.py'])